package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.MaintenanceConfig;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface MaintenanceConfigDao {

    /**
     * 根据 type查询
     */
    @Results(id = "maintenanceConfigResultMap", value = {
            @Result(column = "maintenance_config_id", property = "maintenanceConfigId", jdbcType = JdbcType.INTEGER),
            @Result(column = "maintenance_config_range1", property = "maintenanceConfigRange1", jdbcType = JdbcType.DOUBLE),
            @Result(column = "maintenance_config_range2", property = "maintenanceConfigRange2", jdbcType = JdbcType.DOUBLE),
            @Result(column = "maintenance_config_value", property = "maintenanceConfigValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "maintenance_config_type", property = "maintenanceConfigType", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "forewarning_age", property = "forewarningAge", jdbcType = JdbcType.DOUBLE),
            @Result(column = "forewarning_age_undisposed", property = "forewarningAgeUndisposed", jdbcType = JdbcType.INTEGER),
            @Result(column = "forewarning_age_disposed", property = "forewarningAgeDisposed", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "upkeep_advance_or_delay", property = "upkeepAdvanceOrDelay", jdbcType = JdbcType.INTEGER),
            @Result(column = "send_email_users", property = "sendEmailUsers", jdbcType = JdbcType.VARCHAR),
    })
    @Select("select * from " + DB1TableName.MAINTENANCE_CONFIG + " where maintenance_config_type = #{maintenanceConfigType} order by sort")
    List<MaintenanceConfig> getMaintenanceConfigByType(@Param("maintenanceConfigType") Integer maintenanceConfigType);

    /**
     * 根据id查询
     *
     * @param maintenanceConfigId
     * @return
     */
    @ResultMap(value = "maintenanceConfigResultMap")
    @Select("select * from " + DB1TableName.MAINTENANCE_CONFIG + " where maintenance_config_id = #{maintenanceConfigId}")
    MaintenanceConfig getMaintenanceConfigById(@Param("maintenanceConfigId") Integer maintenanceConfigId);

    /**
     * 更新
     *
     * @param maintenanceConfig
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.MAINTENANCE_CONFIG +
            "<set>" +
            "<if test='maintenanceConfig.maintenanceConfigValue != null and maintenanceConfig.maintenanceConfigValue != \"\" '>" +
            " maintenance_config_value = #{maintenanceConfig.maintenanceConfigValue}," +
            "</if>" +
            "<if test='maintenanceConfig.maintenanceConfigRange1 != null and maintenanceConfig.maintenanceConfigRange1 != \"\" '>" +
            " maintenance_config_range1 = #{maintenanceConfig.maintenanceConfigRange1}," +
            "</if>" +
            "<if test='maintenanceConfig.maintenanceConfigRange2 != null and maintenanceConfig.maintenanceConfigRange2 != \"\" '>" +
            " maintenance_config_range2 = #{maintenanceConfig.maintenanceConfigRange2}," +
            "</if>" +
            "<if test='maintenanceConfig.maintenanceConfigType != null and maintenanceConfig.maintenanceConfigType != \"\" '>" +
            " maintenance_config_type = #{maintenanceConfig.maintenanceConfigType}," +
            "</if>" +
            "<if test='maintenanceConfig.forewarningAge != null and maintenanceConfig.forewarningAge != \"\" '>" +
            " forewarning_age = #{maintenanceConfig.forewarningAge}," +
            "</if>" +
            "<if test='maintenanceConfig.forewarningAgeUndisposed != null and maintenanceConfig.forewarningAgeUndisposed != \"\" '>" +
            " forewarning_age_undisposed = #{maintenanceConfig.forewarningAgeUndisposed}," +
            "</if>" +
            "<if test='maintenanceConfig.forewarningAgeDisposed != null and maintenanceConfig.forewarningAgeDisposed != \"\" '>" +
            " forewarning_age_disposed = #{maintenanceConfig.forewarningAgeDisposed}," +
            "</if>" +
            "<if test='maintenanceConfig.sort != null and maintenanceConfig.sort != \"\" '>" +
            " sort = #{maintenanceConfig.sort}," +
            "</if>" +
            "<if test='maintenanceConfig.upkeepAdvanceOrDelay != null and maintenanceConfig.upkeepAdvanceOrDelay != \"\" '>" +
            " upkeep_advance_or_delay = #{maintenanceConfig.upkeepAdvanceOrDelay}," +
            "</if>" +
            "<if test='maintenanceConfig.updateTime != null '>" +
            " update_time = #{maintenanceConfig.updateTime}," +
            "</if>" +
            "</set>" +
            " where maintenance_config_id = #{maintenanceConfig.maintenanceConfigId}" +
            "</script>")
    Integer updateMaintenanceConfig(@Param("maintenanceConfig") MaintenanceConfig maintenanceConfig);

    /**
     * 更新SendEmailUsers
     */
    @Update("update " + DB1TableName.MAINTENANCE_CONFIG + " set send_email_users = #{sendEmailUsers} where maintenance_config_id = #{maintenanceConfigId}")
    Integer updateSendEmailUsers(@Param("maintenanceConfigId") Integer maintenanceConfigId, @Param("sendEmailUsers") String sendEmailUsers);
}
